﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 缺陷服务，上报后可以修改，但修改后上报状态需要更新为“未上报”，保证可以继续上报
    /// </summary>
    public class FlawService
    {
        /// <summary>
        /// 通过设备ID和登录用户获取对应的缺陷
        /// </summary>
        /// <param name="deviceID"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<Flaw> GetFlawsByDevice(string deviceID,string userName)
        {
            return AppLogic.dataService.Find<Flaw>("DeviceId = '" + deviceID + "' and UserName = '" + userName + "'", "DisCoverDateTime");
        }

        /// <summary>
        /// 获取登录用户的所有缺陷
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<Flaw> GetAllFlaws(string userName)
        {
            return AppLogic.dataService.Find<Flaw>("UserName = '" + userName + "'", "DisCoverDateTime");
        }

        /// <summary>
        /// 增加一个缺陷
        /// </summary>
        /// <param name="flaw"></param>
        /// <returns></returns>
        public bool AddFlaw(Flaw flaw)
        {
            return AppLogic.dataService.Add<Flaw>(flaw) == 1;
        }

        /// <summary>
        /// 更新一个缺陷
        /// </summary>
        /// <param name="flaw"></param>
        /// <returns></returns>
        public bool UpdateFlaw(Flaw flaw)
        {
            return AppLogic.dataService.Update<Flaw>(flaw) == 1;
        }

        /// <summary>
        /// 删除缺陷，下载的待消缺的缺陷不允许更新
        /// </summary>
        /// <param name="flawID"></param>
        /// <returns></returns>
        public bool DeleteFlaw(string flawID)
        {
            return  AppLogic.dataService.Delete<Flaw>(flawID) == 1;
        }

        /// <summary>
        /// 更新缺陷上报状态
        /// </summary>
        /// <param name="flaws">缺陷列表</param>
        /// <param name="newState">新状态</param>
        /// <returns></returns>
        public void UpdateReportState(List<Flaw> flaws, string newState)
        {
            //更新缺陷信息上报状态
            LinkFileService linkFileService = new LinkFileService();
            foreach (Flaw flaw in flaws)
            {
                flaw.ReportState = newState;
                UpdateFlaw(flaw);
            }

            
        }

        /// <summary>
        /// 获取一个缺陷
        /// </summary>
        /// <param name="flawID"></param>
        /// <returns></returns>
        public Flaw GetFlaw(string flawID)
        {
            return AppLogic.dataService.GetEntityByKey<Flaw>(flawID);
        }

        /// <summary>
        /// 上报单个缺陷到服务器
        /// </summary>
        /// <param name="flawID"></param>
        /// <returns></returns>
        public bool ReportFlawToServer(string flawID)
        {
            if (AppLogic.netService.CurrentNetType != NetType.None)
            {
                Flaw flaw = GetFlaw(flawID);
                if (flaw == null)
                    return true;

                Synchronize synchronize = new Synchronize();
                List<Flaw> flaws = new List<Flaw>();
                flaws.Add(flaw);
                synchronize.UploadFlaws(flaws);
            }
            return true;
        }

        /// <summary>
        /// 获取需要上传的缺陷
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<Flaw> GetNeedReportFlaw(string userName)
        {
            return AppLogic.dataService.Find<Flaw>("UserName = '" + userName + "' and (DealState='已验收' or DealState='退回' or (DealState ='草稿' and ReportState <> '已上报'))", "");

        }

        /// <summary>
        /// 验收待消缺的缺陷
        /// </summary>
        /// <param name="flawID"></param>
        /// <returns></returns>
        public bool AcceptanceFlaw(string flawID)
        {
            Flaw flaw = GetFlaw(flawID);

            if (flaw == null)
                return false;

            flaw.DealState = "已验收";

            UpdateFlaw(flaw);

            return true;
        }

        /// <summary>
        /// 驳回缺陷
        /// </summary>
        /// <param name="flawID"></param>
        /// <returns></returns>
        public bool RejectFlaw(string flawID)
        {
            Flaw flaw = GetFlaw(flawID);

            if (flaw == null)
                return false;

            flaw.DealState = "退回";

            UpdateFlaw(flaw);

            return true;
        }

        /// <summary>
        /// 处理新接收到的缺陷
        /// </summary>
        /// <param name="flaws"></param>
        public void DealNewReceivedFlaw(List<Object> flaws)
        {
            foreach (Flaw flaw in flaws)
            {
                if (AppLogic.dataService.Add<Flaw>(flaw) != 1)
                {
                    //已经存在的缺陷，如果状态为“已验收、退回”，不修改，否则修改
                    Flaw oldFlaw = GetFlaw(flaw.FlawID);
                    if (oldFlaw != null &&oldFlaw.DealState != "已验收" && oldFlaw.DealState != "退回")
                        AppLogic.dataService.Update<Flaw>(flaw);
                }
            }
        }

        /// <summary>
        /// 删除已经上报且处理过的待消缺缺陷
        /// </summary>
        public void DeleteReportDealFlaw()
        {
            //删除缺陷文件
            //先获取需要删除的缺陷
            List<Flaw> flaws = AppLogic.dataService.Find<Flaw>("ReportState ='已上报' and (DealState= '已验收' or DealState = '退回')", "");
            string flawIds = "";
            foreach (Flaw flaw in flaws)
            {
                flawIds += ",'" + flaw.FlawID + "'";
            }
            if (flawIds != "")
                flawIds = flawIds.Remove(0, 1);


            //删除缺陷
            AppLogic.dataService.Delete<Flaw>("ReportState ='已上报' and (DealState= '已验收' or DealState = '退回')", null);

        }


    }
}
